<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 用户
 * @author www.dingwei.cn
 */
class Home extends MY_Controller {
	
	/**
	 * 广告模块
	 * @var AdmAdsModel
	 */
	public $mAds;
	
	/**
	 * 分类模块
	 * @var ConsumerCategoryModel
	 */
	public $mCategory;
	
	/**
	 * 分类模块
	 * @var AdmNewsModel
	 */
	public $mNew;
	
	/**
	 * 分类模块
	 * @var MerchantUserModel
	 */
	public $mShop;
	
	/**
	 * 设置模块
	 * @var AdmSettingModel
	 */
	public $mSetting;
	
	/**
	 * 银行模块
	 * @var AdmBankModel
	 */
	public $mBank;
	/**
	 * 模块
	 * @var ConsumerPromotionModel
	 */
	public $mPromotion;
	
	/**
	 * 位置标识
	 * @var array
	 */
	public $ADS_POSTION = [
			1 => 'home_top',//首页顶部
			3 => 'home_middle_left',//中间 左边
			4 => 'home_middle_right_top',//中间 右上
			5 => 'home_middle_right_down',//中间右下

	];
	
	/**
	 * 构造函数
	 */
	public function __construct(){
		parent::__construct();
		$this->loadModel('AdmAdsModel', 'mAds', 'admin');
		$this->loadModel('ConsumerCategoryModel', 'mCategory', 'consumer');
		$this->loadModel('AdmNewsModel', 'mNew', 'admin');
		$this->loadModel('MerchantUserModel', 'mShop', 'merchant');
		$this->loadModel('AdmSettingModel', 'mSetting', 'admin');
		$this->loadModel('AdmBankModel', 'mBank', 'admin');
	}
	
	/**
	 * 首页接口
	 *
	 */
	public function index(){

		/**
		 * 获取广告、轮播图
		 * @var array $tAds
		 */
		$tData = $this->mAds->getAdsList(array(1,2,3,4,5),0,2);

		/* 
		 * 获取分类 一级
		 */
		$tData['category'] = $this->mCategory->getCategoryList(0);

		$this->appReturn(1,'',$tData);
	}

	/**
	 * 获取分类列表
	 */
	public function getCategoryList(){
		
		$parentId = (int)post('parent_id');
		
		if ( ! is_numeric($parentId)){
			$this->appReturn(0,'数据类型int');
		}
        $remove = false;
        if(!$parentId){
		    $remove = true;
        }
		$tA = $this->mCategory->getCategoryList($parentId,false,0, $remove);
		if ( ! $tA){
			$this->appReturn(1,'没有任何分类');
		}
        //在二维数组里增加元素保留原有的健名
        $array = array(array(
            'id' => $parentId > 0?$parentId:'0',
            'cat_name' => '全部',
            'pic' => ''
        ));
        $tA = array_merge($array,$tA);
		$this->appReturn(1,'',$tA);
	}
	
	/**
	 * 获取分类列表
	 */
	public function getCustomCategoryList(){
		//表单验证
		$vConf = array(
		 	array('merchant_id', '商家id', 'required|is_natural_no_zero'),//1-首页 2-商盟
		 );
		 $this->formDataValidation($vConf);
		 
		$merchantId = (int)post('merchant_id');
		$parentId = (int)post('parent_id');
		
		if ( ! is_numeric($parentId)){
			$this->appReturn(0,'数据类型int');
		}

		$tA = $this->mCategory->getCustomList($merchantId,$parentId);
		
		if ( ! $tA){
			$this->appReturn(0,'数据不存在');
		}
		$recommend = array(
				'id'=>'recommend', //-死数据
				'cat_name'=>'推荐商品',
		);
		array_unshift($tA,$recommend);
		$this->appReturn(1,'',$tA);
	}

    /**
     *  关键词搜索
     */
    public function searchByKeyword(){
        //表单验证
        $vConf = array(
            array('keyword', '关键词', 'required'),
            array('latitude', '坐标', 'required'),
            array('longitude', '坐标', 'required'),
        );
        $merchantId = post('id');
        if ($merchantId) {
            $vConf[] = array('id', '店铺id', 'is_natural_no_zero');
        }
        $this->formDataValidation($vConf);

        $keyword = trim(post('keyword'));
        $longitude = trim(post('longitude'));
        $latitude = trim(post('latitude'));

        if ($merchantId){
            //获取商品
            $tProduct = $this->mShop->searchProductByKeyword($keyword, $latitude, $longitude, $merchantId);
            $tShop = array();
        }else {
            //获取店铺
            $tShop = $this->mShop->searchShopByKeyword($keyword, $latitude, $longitude);
            //获取商品
            $tProduct = $this->mShop->searchProductByKeyword($keyword, $latitude, $longitude);
        }
        $data = array(
            'product' => $tProduct,
            'shop' => $tShop,
        );

        $this->appReturn(1, '', $data);
    }
	
	/**
	 * 获取店铺列表
	 */
	public function getShopList(){
		//表单验证
		$vConf = array(
				array('sort', '排序类型', 'required|is_natural_no_zero'),
				array('category_id', '分类id', 'is_numeric'),
				array('page', '分页', 'required|is_natural_no_zero'),
				
		);
		$this->formDataValidation($vConf);
		
		
		$sort 			= (int)post('sort');	//排序规则 可选
		$categoryId 	= trim(post('category_id')); //分类
		$page 			= trim(post('page'));//分页
		$limit 			= trim(post('limit'));
		$keyword 		= trim(post('keyword'));
		$latitude 		= trim(post('latitude'));
		$longitude 		= trim(post('longitude'));
		
		if ( ! in_array($sort, array(1,2,3,4))){
			$this->appReturn(0,'排序错误');
		}
		
		//验证分类是否存在
		if ($categoryId){
			if ( ! $this->mCategory->getInfo($categoryId)){
				$this->appReturn(0,'分类不存在');
			}
		}
		
		//获取店铺
		$tShop = $this->mShop->appGetShopList($categoryId,$sort,0,$page,array(),false,$keyword,$latitude,$longitude,$limit);
		
		$this->appReturn(1,'',$tShop);
	}
	
	/**
	 * 获取地址
	 */
	public function getLocation(){
		//表单验证
		$vConf = array(
				array('parent_id', '上级id', 'required|is_natural_no_zero'),
				
		);
		$this->formDataValidation($vConf);
		
		$parentId = trim(post('parent_id'));
		
		$tA = $this->mSetting->getLocationByPid($parentId);
		
		$this->appReturn(1,'',$tA);
	}
	
	/**
	 * 获取开户行
	 */
	public function getBankList(){
		$tA = $this->mBank->getList();
		$this->appReturn(1,'',$tA);
	}
	
	/**
	 * 获取分享参数
	 */
	public function getSharp(){
		$tA = $this->mSetting->get('sharp_title,sharp_icon,sharp_url,sharp_desc');
		$tA['sharp_icon'] = getImage($tA['sharp_icon'],'og');
		//$tA['sharp_url'] = getUrl('app/home/share');
		$this->appReturn(1,'',$tA);
	}

	/**
	 * 分享下载页面
	 */
    public function share(){
        $tA = $this->mSetting->get('
									app_merchant_android_download_url, 
									app_consumer_android_download_url,
									app_consumer_ios_download_url,
									app_merchant_ios_download_url');

        $dln = $this->mSetting->get('android_update_url,ios_update_url,', 'deliveryman');

        //专题信息
        $this->db->select('down_img,content');
        $share = $this->mSetting->getOne($this->mSetting->tbShare);

        $img = base_url($share['down_img']);


        $this->view('share/share',[
                'android_consumer'=>$tA['app_consumer_android_download_url'],
                'android_merchant'=>$tA['app_merchant_android_download_url'],
                'android_update'=>$dln['android_update_url'],
                'ios_consumer'=>$tA['app_consumer_ios_download_url'],
                'ios_merchant'=>$tA['app_merchant_ios_download_url'],
                'ios_update'=>$dln['ios_update_url'],
                'img'=>$img,
            ]
        );
    }
	
    /**
     * 分享成功赠送积分
     * @author heyu
     */
    public function giftIntegral(){
        $this->loadModel('ConsumerExperienceValueModel', 'mExperience','consumer');
        $this->loadModel('ConsumerMallModel', 'mMall','consumer');
        $this->mMall->_addIntegral(5, $this->userId, $this->userIntegral);
        $this->mExperience->_addExperienceValue(4, $this->userId, $this->userExperienceValue);
        $this->ajaxReturn(1, '赠送成功');
    }

	/**
	 * 关于我们
	 */
	public function aboutUs(){
		$tA = $this->mSetting->getExplain('about_us');
		$tA = str_replace('{base_url}', base_url(), $tA);
		$this->appReturn(1,'',$tA);
	}

    /**
     * 帮助中心
     */
    public function helpUs(){
        $tA = $this->mSetting->getExplain('help_us');
        $tA = str_replace('{base_url}', base_url(), $tA);
        $this->appReturn(1,'',$tA);
    }

    /**
     * 用户引导广告图
     */
    public function ads(){

        $config = $this->mSetting->get('consumer_ads,app_merchant_android_download_url');
        $tA = array();
        if($config['consumer_ads']){
            $tA = json_decode($config['consumer_ads'], true);
            foreach ($tA as $k=>$v){
                $tA[$k] = base_url($v);
            }
        }
        $data = array(
            'ads' => $tA,
            'url' => base_url().'consumer/app/home/share',
        );
        $this->appReturn(1, '', $data);
    }

	/**
	 * 注册协议
	 */
	public function agreement(){
		$tA = $this->mSetting->getExplain('agreement');
		$tA = str_replace('{base_url}', base_url(), $tA);
		$this->appReturn(1,'',$tA);
	}

	/**
	 * 获取积分说明
	 */
	public function getIntegralDesc(){
		$info = $this->mSetting->getExplain('integral_desc');
		$this->appReturn(1,'',$info);
	}

	/**
	 * 获取红包描述
	 */
	public function getBonusDesc(){
		$info = $this->mSetting->getExplain('bonus_desc');
		$this->appReturn(1,'',$info);
	}
	
	/**
	 * 获取代金劵描述
	 */
	public function getCouponDesc(){
		$info = $this->mSetting->getExplain('coupon_desc');
		$this->appReturn(1,'',$info);
	}
	
	/**
	 * 获取计费规则
	 */
	public function getFeeDesc(){
	    $info = $this->mSetting->getExplain('distribution_fee_desc');
        $info = '<style type="text/css">.rImg img{width: 100%}</style><div class="rImg">'.$info.'</div>';
	    $this->appReturn(1,'',$info);
	}
	
	/**
	 * 获取提现说明
	 */
	public function getWithdrawDesc(){
		$info = $this->mSetting->getExplain('consumer_withdraw_desc');
		$this->appReturn(1,'',$info);
	}
	
	/**
	 * 获取充值说明
	 */
	public function getRechargeDesc(){
		$info = $this->mSetting->getExplain('recharge_desc');
		$this->appReturn(1,'',$info);
	}

	/**
	 * 获取包邮卡说明
	 */
	public function getPostalCardDesc(){
		$info = $this->mSetting->getExplain('postal_card_desc');
		$this->appReturn(1,'',$info);
	}
	
	/**
	 * 获取申请售后原因
	 */
	public function getAftersaleReason(){
		$info = $this->mSetting->get('aftersale_reason');
		$this->appReturn(1,'',json_decode($info,true));
	}

	/**
	 * 获取店铺/配送员评论标签
	 */
	public function getCommentTags(){
		$m = $this->mSetting->get('merchant_comment_tags');
        $d = $this->mSetting->get('deliveryman_comment_tags', 'deliveryman');
		$data = array(
		    'merchant' => json_decode($m,true),
		    'deliveryman' => json_decode($d,true),
        );
		$this->appReturn(1,'', $data);
	}
	
	/**
	 * 平台获取基本信息
	 */
	public function getBase(){
		$info = $this->mSetting->get('web_service_tel,web_service_qq,hot_search');
		$this->appReturn(1,'',[
				'web_service_tel'=>$info['web_service_tel'],
				'web_service_qq'=>$info['web_service_qq'],
				'hot_search'=>json_decode($info['hot_search']),
		]);
	}

	
}